package com.example.demo.web;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    UserRepository userRepository;

    @GetMapping("/{id}")
    public User get(@PathVariable int id){
        return userRepository.findById(id).get();
    }

    @PostMapping("")
    public User create(@RequestBody User user) {
        return userRepository.save(user);
    }

    @PutMapping("")
    public User update(@RequestBody User user) {
        return userRepository.save(user);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        userRepository.deleteById(id);
    }


}
